﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Enterprise.Model.ZhiWu;
using Enterprise.Data.ZhiWu;
using Enterprise.Infrastructure;
using System.Data;
namespace Enterprise.Service.ZhiWu
{
    /// <summary>
    ///  职务服务
    /// </summary>
    public class ZhiwuService
    {

        /// <summary>
        /// 用户缓存标记
        /// </summary>
        private static string cacheKey = "Enterprise-Zhiwu";

        /// <summary>
        /// 列表 返回所有记录
        /// </summary>
        /// <returns></returns>
        public List<ZhiwuEntity> ZhiWuList()
        {
            if (CacheHelper.Contains(cacheKey))
            {
                return (List<ZhiwuEntity>)CacheHelper.GetCache(cacheKey);
            }
            else
            {
                ZhiwuData data = new ZhiwuData();
                List<ZhiwuEntity> list = data.List();
                CacheHelper.Add(cacheKey, list);
                return list;
            }
        }

        /// <summary>
        /// 返回记录信息
        /// </summary>
        /// <param name="Id">编码</param>
        /// <returns></returns>
        public ZhiwuEntity ZhiWuDisp(int Id)
        {
            return ZhiWuList().Where(p => p.ZWID == Id).FirstOrDefault();
        }

        private void ClearCache()
        {
            CacheHelper.RemoveCache(cacheKey);
        }

        /// <summary>
        /// 操作 添加、修改、删除
        /// </summary>
        /// <param name="entity">用户实体</param>
        public void ZhiWuExecute(ZhiwuEntity entity)
        {
            string cmd = string.Empty;
            switch (entity.DB_Option_Action)
            {
                case "Insert":
                    cmd = "Insert into SYS_ZHIWU(ZWID,ZWNAME,ZWREMARK) values(SEQ_SYS_ZHIWU.nextval,'" + entity.ZWNAME + "','" + entity.ZWREMARK + "')";
                    break;
                case "Update":
                    cmd = "Update SYS_ZHIWU set ZWNAME='" + entity.ZWNAME + "' , ZWREMARK='" + entity.ZWREMARK + "' where ZWID=" + entity.ZWID;
                    break;
                case "Delete":
                    cmd = "Delete from SYS_ZHIWU where ZWID=" + entity.ZWID;
                    break;
            }
            if (!string.IsNullOrEmpty(cmd))
            {
                ZhiWuExecuteBySql(cmd, 1);
            }
        }

        /// <summary>
        /// 操作方法
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="typeId">1:刷新缓存 0:不刷新缓存 </param>
        public void ZhiWuExecuteBySql(string cmd, int typeId)
        {
            ZhiwuData data = new ZhiwuData();
            data.Execute(cmd);
            if (typeId == 1)
            {
                CacheHelper.RemoveCache(cacheKey);
            }
        }

        #region "静态方法"
        /// <summary>
        /// 返回职务名称
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static string GetZhiWuName(int Id)
        {
            ZhiwuService service = new ZhiwuService();
            var q = service.ZhiWuDisp(Id);
            if (q != null)
            {
                return q.ZWNAME;
            }
            return "";
        }
        #endregion
    }
}
